<template>
  <div>
    <el-upload
      action="#"
      list-type="picture-card"
      :on-preview="handlePictureCardPreview"
      :on-remove="handleRemove"
      :limit="1"
      :file-list="fileList"
      :class="{disabled: fileComputed }"
      :on-change="changeFile"
      :before-upload="beforeUpload"
      :http-request="upload"
    >
      <i class="el-icon-plus" />
      <el-progress v-if="showPercent" style="width: 180px" :percentage="percent" />
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>

</template>

<script>
import COS from 'cos-js-sdk-v5'
const soc = new COS({
  SecretId: 'AKIDiIdlfKkupX0FG2MSJwLUQ7apsg3FDwXw',
  SecretKey: 'FKCsMcfU6UfdOuCH8nESUK2XoshIgroz'
})
export default {
  data() {
    return {
      fileList: [], // 图片地址设置为数组
      dialogImageUrl: '',
      dialogVisible: false,
      showPercent: false,
      percent: 0,
      currentFileUid: null
    }
  },
  computed: {
    // 设定一个计算属性 判断是否已经上传完了一张
    fileComputed() {
      return this.fileList.length === 1
    }
  },
  methods: {
    handleRemove(file, fileList) {
      this.fileList = this.fileList.filter(item => item.uid !== file.uid)
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    changeFile(file, fileList) {
      this.fileList = fileList.map(item => item)
      console.log(fileList)
    },
    beforeUpload(file) {
      // 要开始做文件上传的检查了
      // 文件类型 文件大小
      const types = ['image/jpeg', 'image/gif', 'image/bmp', 'image/png']
      if (!types.includes(file.type)) {
        this.$message.error('上传图片只能是 JPG、GIF、BMP、PNG 格式!')
        return false
      }
      //  检查大小
      const maxSize = 5 * 1024 * 1024
      if (maxSize < file.size) {
        this.$message.error('图片大小最大不能超过5M')
        return false
      }
      //   已经确定当前上传的就是当前的这个file了
      this.currentFileUid = file.uid
      this.showPercent = true
      return true
    },
    // 自定义上传动作 有个参数 有个file对象，是我们需要上传到腾讯云服务器的内容
    upload(params) {
      console.log(params.file)
      if (params.file) {
        soc.putObject({
          Bucket: 'yanlonghanshui-01-1312849118', // 存储桶名称
          Region: 'ap-nanjing', // 地域
          Key: params.file.name, // 文件名
          Body: params.file, // 要上传的文件对象
          StorageClass: 'STANDARD', // 上传的模式类型 直接默认 标准模式即可
          onProgress: (params) => {
            this.percent = params.percent * 100
          }
        }, (err, data) => {
          console.log(err, data)
          if (!err && data.statusCode === 200) {
            this.fileList = this.fileList.map(item => {
              if (item.uid === this.currentFileUid) {
                return {
                  url: `http://${data.Location}`, upload: true
                }
              }
              return item
            })
            this.showPercent = false
            this.percent = 0
          }
        })
      }
    }

  }
}
</script>

<style>
.disabled .el-upload--picture-card {
  display: none
}
</style>
